package com.lokiy.kit.fragment

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.transition.ChangeBounds
import androidx.transition.ChangeImageTransform
import androidx.transition.Scene
import androidx.transition.TransitionManager
import com.lokiy.annotation.FragmentInfo
import com.lokiy.core.fragment.ViewBindingFragment
import com.lokiy.kit.R
import com.lokiy.kit.databinding.FragmentTransitionManagerBinding
import com.lokiy.kit.ext.setupToolbar
import com.lokiy.kit.widget.ProgressTransition
import kotlin.random.Random

/**
 *
 * @author Lokiy
 * @date 2023-09-04 10:19
 */
@FragmentInfo("TransitionManager")
class TransitionManagerFragment : ViewBindingFragment<FragmentTransitionManagerBinding>() {

    private val scene1 by lazy { Scene.getSceneForLayout(binding.container, R.layout.fragment_transition_manager_scene1, requireContext()) }
    private val scene2 by lazy { Scene.getSceneForLayout(binding.container, R.layout.fragment_transition_manager_scene2, requireContext()) }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        setupToolbar(binding.toolbar)
        binding.btnProgress.setOnClickListener {
            setProgress(Random.nextInt(100))
        }
        binding.btnItem.setOnClickListener {
            val nextInt = Random.nextInt(3)
            TransitionManager.endTransitions(binding.root)
            when (nextInt) {
                1 -> {
                    binding.item1.isVisible = false
                    binding.item2.isVisible = true
                }

                2 -> {
                    binding.item1.isVisible = false
                    binding.item2.isVisible = false
                }

                else -> {
                    binding.item1.isVisible = true
                    binding.item2.isVisible = true
                }
            }
            TransitionManager.beginDelayedTransition(binding.root)
        }
        binding.btnScene.setOnClickListener {
            if (it.tag == null) {
                it.tag = 1
                TransitionManager.go(scene1, ChangeBounds())
            } else {
                it.tag = null
                TransitionManager.go(scene2, ChangeBounds())
            }
        }
        binding.btnImage.setOnClickListener {
            if (binding.root.findViewById<ImageView>(R.id.image1) == null) {
                TransitionManager.go(scene1, ChangeBounds())
            }
            TransitionManager.beginDelayedTransition(binding.container, ChangeImageTransform().setDuration(1000))
            if (it.tag == null) {
                it.tag = 1
                scene1.sceneRoot.findViewById<ImageView>(R.id.image1)?.setScaleType(ImageView.ScaleType.FIT_CENTER)
            } else {
                it.tag = null
                scene1.sceneRoot.findViewById<ImageView>(R.id.image1)?.setScaleType(ImageView.ScaleType.FIT_XY)
            }
        }
    }

    private fun setProgress(value: Int) {
        TransitionManager.beginDelayedTransition(binding.root, ProgressTransition())
        binding.progressBar.progress = 0.coerceAtLeast(100.coerceAtMost(value))
    }
}